/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;
/**
 * 说明：
 * 仓库调拨
 * @Author：
 * @Date：2012-03-21
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrder;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.OrderManageModel;
import com.ruipengkj.service.TOrderService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.orderView.OrderDetailDialog;

public class OrderManagePanel extends BasePagePanel<TOrder> implements Runnable {
	private static final long serialVersionUID = 1L;
	private JLabelJTextFieldComponent orderNum; //预订单号
	private JLabelJComboBoxComponent orderStatus; //订单状态
	private JLabelJComboBoxComponent orderSource;//订单来源
	private DataPackageComponent startTime; //开始时间
	private DataPackageComponent endTime;	//结束时间
	
	private JLabelJTextFieldComponent usernameTF;//用户名
	private JLabelJTextFieldComponent mobileTF;//手机号码
	
	private JButton queryButton;//查询
	private JButton clearButton;//清空
	private JButton showButton;//查看详细
	private JButton sureOrderButton; //确认订单
	private JButton sendOrderButton; //发货
	private JButton changePriceButton; //订单改价
	private JButton noSendButton; //不可达
//	private JButton chargeButton; //充值
//	private JButton noticeButton; //通知
//	private JButton memberDetailButton; //查看会员行为
//	private JButton showButton; //查看详细
//	private JButton dGiftButton; //兑换礼品
	//private JButton addButton; //添加进货单
	private JTable table;
	private JScrollPane tableJsp;
	private OrderManageModel orderManageModel;
	private ChangePriceDialog changePriceDialog;//通知会员
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TOrder> pageView;//翻页主键
//	private TUserService tUserService;//仓库调拨操作对象
	private TOrderService tOrderService;
	public OrderManagePanel() {
		//tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		tOrderService=BuildSpringApplication.getInstance().getTOrderServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		this.termQuery(1);
	}
	private void init() {
		pageView = new PageView<TOrder>();
		orderManageModel = new OrderManageModel();
		table=uiFactory.createJTable(orderManageModel, null, this, 30, JLabel.CENTER);
		tableJsp=new JScrollPane(table);
		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(0).setCellEditor(table.getDefaultEditor(Boolean.class));
		columnModel.getColumn(0).setCellRenderer(table.getDefaultRenderer(Boolean.class));
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		this.add(this.initTop(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(this.initTail(), BorderLayout.SOUTH);
	}
	
	private JPanel initTop() {
		JPanel top_panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		orderNum=new JLabelJTextFieldComponent("订单编号：", 15);
		usernameTF=new JLabelJTextFieldComponent("用户名：", 15);
		mobileTF=new JLabelJTextFieldComponent("手机号：", 15);
		orderStatus=new JLabelJComboBoxComponent("订单状态：",ApplicationContext.ORDER_STATUS_MAP(), 80);
		orderSource=new JLabelJComboBoxComponent("订单来源：",ApplicationContext.ORDER_SOURCE_MAP() ,80);
		startTime=new DataPackageComponent("预订时间：", 10);
		endTime=new DataPackageComponent("时间：", 10);
		queryButton = new JButton("查询");
		clearButton= new JButton("清空");
		top_panel.add(orderNum);
		top_panel.add(usernameTF);
		top_panel.add(mobileTF);
		top_panel.add(orderStatus);
		top_panel.add(orderSource);
		top_panel.add(startTime);
		top_panel.add(endTime);
		top_panel.add(queryButton);
		top_panel.add(clearButton);
		return top_panel;
	}
	
	private JPanel initTail() {
		//addButton = new JButton("添加调拨单(Ctrl+A)");
		showButton = new JButton("查看详细");
		sureOrderButton= new JButton("确认订单");
		sendOrderButton = new JButton("发货");
		changePriceButton=new JButton("订单改价");
		noSendButton=new JButton("不可达");
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{showButton,sureOrderButton,sendOrderButton,changePriceButton,noSendButton}, FlowLayout.RIGHT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{pagingComponent,jp2}, 2, 1);
		return tail_panel;
	}
	private void initListener(){
		clearButton.addActionListener(this);
		queryButton.addActionListener(this);
		//addButton.addActionListener(this);
		sureOrderButton.addActionListener(this);
		sendOrderButton.addActionListener(this);
		changePriceButton.addActionListener(this);
		noSendButton.addActionListener(this);
		showButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		}else if(e.getSource()==showButton){
			//查看详细
			if (table.getSelectedRow() != -1) {
				String orderID = (String) table.getValueAt(table.getSelectedRow(), 1);
				OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "订单详细", true);
				orderDetailDialog.showDialog(orderID);
			} else {
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==sureOrderButton){
			//确认订单
			if(table.getSelectedRow()!=-1){
				boolean[] bools = orderManageModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
					try {
						TOrder tOrder = new TOrder();
						boolean ISRun=false;
						List<TOrder> tOrderList = orderManageModel.getTCommodityList();
						//List<TOrder> tUserListOn = new ArrayList();
						for(int i=0;i<tOrderList.size();i++){
							if (bools[i]) {
								if(tOrderList.get(i).getStatus() >2)
								{
									ISRun=false;
									break;
								}else{
									ISRun=true;
								}
							}
						}
						for(int i=0;i<tOrderList.size();i++){
							if (bools[i]) {
							if(ISRun)
							{
								tOrder=tOrderList.get(i);
								tOrder.setStatus((byte)2);
								tOrderService.modifyService(tOrder);
							}else{
								JOptionPane.showMessageDialog(null, "请检查选中订单的状态！");
								break;
							}
							}
						}
						table.updateUI();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==sendOrderButton){
			//发货
			if(table.getSelectedRow()!=-1){
					boolean[] bools = orderManageModel.getBools();
					boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
					for (int i = bools.length -1 ; i >= 0; --i) {
						if (bools[i]) {
							select = true;
							break;
						}
					}
					if (!select) {
						JOptionPane.showMessageDialog(null, "请先选择用户！");
						return;
					} else {
						try {
							TOrder tOrder = new TOrder();
							boolean ISRun=false;
							List<TOrder> tOrderList = orderManageModel.getTCommodityList();
							for(int i=0;i<tOrderList.size();i++){
								if (bools[i]) {
									if(tOrderList.get(i).getStatus() >5 || tOrderList.get(i).getStatus() <2)
									{
										ISRun=false;
										break;
									}else{
										ISRun=true;
									}
								}
							}
							for(int i=0;i<tOrderList.size();i++){
								if (bools[i]) {
								if(ISRun)
								{
									tOrder=tOrderList.get(i);
									tOrder.setStatus((byte)5);
									tOrderService.modifyService(tOrder);
								}else{
									JOptionPane.showMessageDialog(null, "请检查选中订单的状态！");
									break;
								}
								}
							}
							table.updateUI();
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==changePriceButton){
			if(table.getSelectedRow()!=-1){
				String orderID = (String)table.getValueAt(table.getSelectedRow(), 1);
				try {
					
					TOrder tOrder= tOrderService.getByIdService(orderID);
					if(tOrder.getStatus()>=2 && tOrder.getStatus()<=5){
						changePriceDialog = new ChangePriceDialog(null, "订单改价", true);
						changePriceDialog.showDialog(tOrder);
						if(changePriceDialog.isSuccess==true){
							this.termQuery(1);
						}
					}else{
						JOptionPane.showMessageDialog(null, "订单状态已过！");
					}
					
					table.updateUI();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==noSendButton){
			//不可达
			if(table.getSelectedRow()!=-1){
				boolean[] bools = orderManageModel.getBools();
				boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
				for (int i = bools.length -1 ; i >= 0; --i) {
					if (bools[i]) {
						select = true;
						break;
					}
				}
				if (!select) {
					JOptionPane.showMessageDialog(null, "请先选择用户！");
					return;
				} else {
					try {
						TOrder tOrder = new TOrder();
						boolean ISRun=false;
						List<TOrder> tOrderList = orderManageModel.getTCommodityList();
						for(int i=0;i<tOrderList.size();i++){
							if (bools[i]) {
								if(tOrderList.get(i).getStatus() ==8)
								{
									ISRun=false;
									break;
								}else{
									ISRun=true;
								}
							}
						}
						for(int i=0;i<tOrderList.size();i++){
							if (bools[i]) {
							if(ISRun)
							{
								tOrder=tOrderList.get(i);
								tOrder.setStatus((byte)5);
								tOrder.setEndWay((byte)5);
								tOrderService.modifyService(tOrder);
							}else{
								JOptionPane.showMessageDialog(null, "请检查选中订单的状态！");
								break;
							}
							}
						}
						table.updateUI();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==clearButton){
			//清空
			orderNum.getText().setText("");
			usernameTF.getText().setText("");
			mobileTF.getText().setText("");
			orderStatus.getBox().setSelectedIndex(0);
			orderSource.getBox().setSelectedIndex(0);
			startTime.setDate(null);
			endTime.setDate(null);
		}
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		//查看详细
		if (e.getSource()==table &&e.getClickCount() == 2) {
			String orderID = (String) table.getValueAt(table.getSelectedRow(), 1);
			OrderDetailDialog orderDetailDialog = new OrderDetailDialog(null, "订单详细", true);
			orderDetailDialog.showDialog(orderID);
		}
    }
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage){
		final TOrder tOrder=new TOrder();
		tOrder.setOrderID(orderNum.getText().getText().trim());
		tOrder.setHappenTime(startTime.getDate());
		tOrder.setEndTime(endTime.getDate());
		tOrder.setSource((Byte)orderSource.getKeyByVaule(orderSource.getBox().getSelectedItem().toString()));
		tOrder.setStatus((Byte)orderStatus.getKeyByVaule(orderStatus.getBox().getSelectedItem().toString()));
		TUser user=new TUser();
		user.setMobile(mobileTF.getText().getText().trim());
		user.setUserName(usernameTF.getText().getText().trim());
		tOrder.setTUser(user);
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TOrder> pageModel = tOrderService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tOrder);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						orderManageModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						waitComponent.setText("不存在数据");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TOrder> getPageView() {
		return pageView;
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}